package controller

import (
	"Last/model"
	"Last/utils"
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"net/http"
	"strconv"
)

// 表格数据相关的放到这里
func ListPart(c *gin.Context) {
	//初始化数据库的 gin
	db := utils.OpenConn()
	defer utils.CloseConn(db)
	db.AutoMigrate(&model.ListData{})
	//获取前端传过来的页码数和每页条数
	pageNum, _ := strconv.Atoi(c.Query("pageNum"))
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	fmt.Println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@", pageNum, pageSize)
	//计算数据总数和页码总数
	var total int
	db.Model(&model.ListData{}).Count(&total)
	pageNumber := total / pageSize
	if total%pageSize != 0 {
		pageNumber++
	}
	//查询分页的数据
	var listDatas []model.ListData
	if err := db.Limit(pageSize).Offset((pageNum - 1) * pageSize).Find(&listDatas).Error; err != nil {
		c.JSON(http.StatusUnauthorized, gin.H{
			"err_msg": err.Error(),
			"message": "查询数据异常",
		})
		return
	}
	fmt.Println(listDatas)
	//成功时的返回数据
	c.JSON(200, gin.H{
		"code":        200,
		"msg":         "查询成功",
		"dataList":    listDatas,
		"totalCount":  total,
		"page_number": pageNumber,
	})
	return
}
func SearchGoods(c *gin.Context) {
	db := utils.OpenConn()
	defer utils.CloseConn(db)
	goodsName := c.Query("goodsName")
	var goodsDate model.ListData
	result := db.Find(&goodsDate, "name=?", goodsName)
	if result.RowsAffected == 0 {
		c.JSON(200, gin.H{
			"code":    -5,
			"message": "查询数据无果！",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code":     200,
			"dataList": goodsDate,
		})
	}
}
func InsertGoods(c *gin.Context) {
	db := utils.OpenConn()
	defer utils.CloseConn(db)
	var InsertData model.ListData
	err := c.BindJSON(&InsertData)
	checkErr(err)
	fmt.Println(InsertData)
	if err = db.Create(&InsertData).Error; err != nil {
		c.JSON(200, gin.H{
			"code":  500,
			"error": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    200,
		"message": "success",
		"data":    InsertData,
	})
}
func UpdateGoods(c *gin.Context) {
	db := utils.OpenConn()
	defer utils.CloseConn(db)
	var UpdateData model.ListData
	err := c.BindJSON(&UpdateData)
	checkErr(err)
	fmt.Println(UpdateData)
	if err = db.Save(&UpdateData).Error; err != nil {
		c.JSON(200, gin.H{
			"code":  500,
			"error": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    200,
		"message": "success",
		"data":    UpdateData,
	})
}
func TitleAll(c *gin.Context) {
	db := utils.OpenConn()
	defer utils.CloseConn(db)
	UserName := c.Query("userName")
	fmt.Println(UserName)
	var DecTitle []model.DecTitle
	result := db.Find(&DecTitle, "teacher1=?", UserName)
	if result.RowsAffected == 0 {
		c.JSON(200, gin.H{
			"code":    -5,
			"message": "查询数据无果！",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code":     200,
			"dataList": DecTitle,
		})
	}
}

//	func DeleteGoods(c *gin.Context) {
//		db := utils.OpenConn()
//		defer utils.CloseConn(db)
//		var UserName = c.Query("userName")
//		if err := db.Delete(&model.ListData{}, ID).Error; err != nil {
//			c.JSON(200, gin.H{
//				"code":  500,
//				"error": err.Error(),
//			})
//			return
//		}
//		c.JSON(200, gin.H{
//			"code":    200,
//			"message": "success",
//		})
//	}
func Dectitle(c *gin.Context) {
	db := utils.OpenConn()
	defer utils.CloseConn(db)
	var DecTitle model.DecTitle
	err := c.BindJSON(&DecTitle)
	checkErr(err)
	fmt.Println(DecTitle)
	if err = db.Create(&DecTitle).Error; err != nil {
		c.JSON(200, gin.H{
			"code":  500,
			"error": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    200,
		"message": "success",
	})
}
